<?php

class Auth extends CI_Controller {
	function Auth()
	{
		parent::__construct();	
		$this->load->model('users_model', 'users_tbl');
		$this->load->model('student_model');
		$this->load->library('Retrieve');
	}
	function getPath($action = ''){
		return array('controller' => strtolower(__CLASS__), 'action' => strtolower($action));
	}
	function index($st='')
	{	
		$mylogin = $this->session->userdata('mylogin');
		if(empty($mylogin)) {
			$this->login($sts=$st);
		} else {
			redirect($this->config->item('member_base').'student/account','location');
			exit;
		}
	}
	
	function login($st){
		$view = array();
		
		$login = $this->input->post('Login');
		$username = $this->input->post('ap_user');
		$password = $this->input->post('ap_pwd');
		$status = $this->student_model->login($username, $password);
		if(!empty($login))
		{
		if($status) {
			$mylogin = $this->session->userdata('mylogin');
			if(empty($_REQUEST['surl'])) 
				redirect($this->config->item('member_base').'student/account','location');
			else 
				redirect($_REQUEST['surl'],'location');
			exit;
		}
		else
				{
				$view['err_msg']	="Login failed";
				}
		}
		elseif($st=='out')
		{
			$view['st_msg']	="You are logged out now";
		}
		$view['surl'] = $_REQUEST['surl'];
		$this->layout->getView('user1/login', $view);
	}
	
	function logout(){
	
		$this->session->sess_destroy();
		redirect($this->config->item('member_base').'auth/index/out','location');
		
	}
	
	function changepwd()
	{
		$mylogin = $this->session->userdata('mylogin');
		$save = $this->input->post('save');
		if(!empty($save)) 
		{
			$name               = $this->input->post('name');
			$password           = $this->input->post('mypassword');
			$confpassword       = $this->input->post('myconfpassword');
			if($password == $confpassword && !empty($password)) {
				$data = array();
				$data['name']                = $name;
				$data['password']            = $password;
                $data['non_enc_password']    = $password;
				$status                      = $this->student_model->changepwd($mylogin['id'], $data);
			}
		}
		$obj = $this->student_model->getStudents(0,1,'',array('users.id' => $mylogin['id']));
		$user = $obj->records[0];
		$view = $this->getPath();
		$view['title'] = "Change Password";
		$view['menu_var']         = 6;
		$view['page_title'] = "Change Password";
		$view['subtitle']	= 'Change Password';
		$view['user'] = $user;
		$view['rurl'] = $_REQUEST['rurl'];
		$view['id'] = $mylogin['id'];
		if($status) $view['msg'] = 'Password Changed Successfully'; 
		$this->layout->memberview('users/changepwd', $view);
		//$this->layout->adminview('users/chg', $view);

	}
	
	function cancel() {
		if(!empty($_REQUEST['rurl'])) {
			redirect($_REQUEST['rurl'],'location');
		} else {
			redirect($this->config->item('admin_base').'users/index','location');	
		}
		
		exit;	
	}
}

/* End of file welcome.php */
/* Location: ./system/application/controllers/welcome.php */